//节流
const throttle = {
  mounted(el, binding) {
    const fn = binding.value;
    
    if (typeof fn !== "function") return;

    el._flag = true; //开关默认为开
    el._timer = null;
    el.handler = function () {
      if (!el._flag) return;
      //执行之后开关关闭
      el._flag && fn();
      el._flag = false;
      if (el._timer !== null) {
        clearTimeout(el._timer);
        el._timer = null;
      }
      el._timer = setTimeout(() => {
        el._flag = true; //2秒后开关开启
      }, 2000);
    };
    el.addEventListener("click", el.handler);
  },
  // 被绑定元素销毁前 移除事件
  beforeUnmount: function (el) {
    el.removeEventListener("click", el.handler);
  },
};
export default throttle;
